package com.mrper.ext.adapter.goods;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.miguo.bean.GoodsBean;
import com.miguo.ui.R;
import com.miguo.ui.databinding.GriditemStaggeredgridGoodsInfoBinding;
import com.mrper.util.DensityUtil;
import com.mrper.util.DeviceUtil;
import com.mrper.util.extra.GoodsUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by admin on 2016/3/11.
 * 九块九模块数据适配器
 */
public class Double9Adapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflator;
    private List<GoodsBean> source;
    private int imageHeight = 0;//图片高度

    public Double9Adapter(Context context){
        this(context,null);
    }

    public Double9Adapter(Context context, List<GoodsBean> source){
        this.context = context;
        this.inflator = LayoutInflater.from(context);
        this.source = source == null?new ArrayList<GoodsBean>():source;
        imageHeight = (int)(DeviceUtil.getScreenWidth(context) - DensityUtil.dip2px(context,21.0f))/2;
    }

    public List<GoodsBean> getSource(){
        return this.source;
    }

    public synchronized void addItem(GoodsBean... goods){
        if(goods!=null && goods.length>0)
            this.source.addAll(Arrays.asList(goods));
    }

    public synchronized void addItem(List<GoodsBean> goods){
        if(goods!=null && goods.size() > 0){
            this.source.addAll(goods);
        }
    }

    public synchronized void addItem(int location,GoodsBean item){
        if(item!=null) this.source.add(location,item);
    }

    public synchronized void removeAt(int position){
        this.source.remove(position);
    }

    public synchronized void remove(GoodsBean item){
        this.source.remove(item);
    }

    public synchronized void clear(){
        this.source.clear();
    }

    public synchronized void syncDataSource(){
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return source == null?0:source.size();
    }

    @Override
    public boolean isEnabled(int position) {
        return source.get(position).getTag() == null;
    }

    @Override
    public Object getItem(int position) {
        return source.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final GoodsBean item = source.get(position);
        final boolean isRefreshTimeType = item.getTag() != null;
        final GriditemStaggeredgridGoodsInfoBinding itemBinding;
        if(convertView == null){
            itemBinding = DataBindingUtil.inflate(inflator,R.layout.griditem_staggeredgrid_goods_info,null,false);
            convertView = itemBinding.getRoot();
            convertView.setTag(itemBinding);
        }else{
            itemBinding = (GriditemStaggeredgridGoodsInfoBinding)convertView.getTag();
        }
        //设置类型
        itemBinding.setIsRefreshTimeType(isRefreshTimeType);
        //设置数据
        if(!isRefreshTimeType){
            itemBinding.setGoodsInfo(item);
            //设置收藏的选项
            final boolean isFavor = GoodsUtil.isFavor(context,item);
            itemBinding.setIsFavor(isFavor);
            itemBinding.imgFavor.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    GoodsUtil.favorActionWidthImageView(context,itemBinding.imgFavor,item,!isFavor);
                    notifyDataSetChanged();//刷新数据
                }
            });
        }else{
            itemBinding.setUpdateTime(item.getTag().toString());
        }
        ViewGroup.LayoutParams imglp = itemBinding.imgGoods.getLayoutParams();
        imglp.height = imageHeight;
        itemBinding.imgGoods.setLayoutParams(imglp);
        itemBinding.executePendingBindings();
        return convertView;
    }

}
